/**
 * Created by Chuch on 2017/3/1.
 */
angular.module('myApp')
    .controller('queryCtrl',['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService','myConstant','dateFilter','DateCalculateService', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                   CacheService,myConstant,dateFilter,DateCalculateService) {

            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};
            vm.queryBean.endTime = DateCalculateService.getToday();
            vm.queryBean.startTime= DateCalculateService.getYesterday();
            vm.cached ={
                BANK_INFO: {},
                T_BANK_CODE:{},
                COMANY_CODE: {},
                BANK_CARD_TYPE: {},
                OPCIF_AGREEMENT_TYPE: {}
            };
            //缓存变量初始化
            CacheService.initCache(vm.cached);
            vm.STATUS_GENDER=[{"key":"M","value":"男"},{"key":"F","value":"女"}];
            vm.APPLY_STATUS=[{"key":"APPLY_SUCCESS","value":"待审核"},{"key":"SUCCESS","value":"认证通过"},{"key":"FAIL","value":"认证失败"}];


            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };
            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.checkStartDay=checkStartDay;
            vm.checkEndDay=checkEndDay;
            vm.getApplyDesc =getApplyDesc;
            vm.totalCount={};
            function  getApplyDesc(status){
                for(var i=0;i<vm.APPLY_STATUS.length;i++){
                    if(vm.APPLY_STATUS[i].key==status){
                        return vm.APPLY_STATUS[i].value;
                    }
                }
            }
            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                //var postData = angular.copy(vm.queryBean);
                if(isNull(vm.queryBean.startTime)|| isNull(vm.queryBean.endTime)){
                    alert("请设置查询时间");
                    return;
                }
                //组织参数：查询条件
                var postData = {};
                postData.startTime = dateFilter(vm.queryBean.startTime, 'yyyyMMdd');
                postData.endTime = dateFilter(vm.queryBean.endTime, 'yyyyMMdd');
                postData.bankCode= vm.queryBean.bankCode;
                //alert(JSON.stringify(vm.queryBean));
                $http.post(vm.url, postData,config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        //alert(JSON.stringify(data));
                        if (data.success) {
                            vm.list = data.object;
                            var suc=0;
                            var fail=0;
                            var sucAmt= 0;
                            var failAmt= 0;
                            for(var i=0;i<vm.list.length;i++){
                                suc+=vm.list[i].paySuccessTotal;
                                fail+=vm.list[i].payFailTotal;
                                sucAmt+=vm.list[i].paySuccessMoney;
                                failAmt+=vm.list[i].payFailMoney;
                            }
                            vm.totalCount.suc =suc;
                            vm.totalCount.fail =fail;
                            vm.totalCount.sucAmt =sucAmt;
                            vm.totalCount.failAmt =failAmt;
                            if( (fail+suc)==0){
                                vm.totalCount.failRate =division(0,1);
                            }else{
                                vm.totalCount.failRate =division(fail,fail+suc);
                            }
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function division(a,b){
                return (a * 100 / b).toFixed(2)+"%";
            }

            function resetForm() {
                vm.queryBean = {};
            }
            function checkStartDay(startDate,endDate) {
                if(!isNull(startDate) && !isNull(endDate)){
                    var startD = new Date(startDate);
                    var endD   = new Date(endDate);
                    if(startD > endD){
                        alert("日期选择错误！请重新选择!");
                        vm.queryBean.startTime = null;
                        vm.queryBean.endTime = DateCalculateService.getToday();
                        return false;
                    }
                }

            }

            function checkEndDay(startDate, endDate) {
                if (!isNull(startDate) && !isNull(endDate)) {
                    var startD = new Date(startDate);
                    var endD = new Date(endDate);
                    if (endD < startD) {
                        alert("日期选择错误！请重新选择!");
                        vm.queryBean.startTime = null;
                        vm.queryBean.endTime = DateCalculateService.getToday();
                        return false;
                    }
                    var days = parseInt((endD.getTime() - startD.getTime()) / (1000 * 60 * 60 * 24));
                    if (days > 92) {
                        alert("日期范围应在92天之内");
                        vm.queryBean.endTime = DateCalculateService.getToday();
                        return false;
                    }
                    return true;
                }
            }

            function isNull(data){
                return (data == "" || data == undefined || data == null);
            }

        }]);